package oakclub.background;

import oakclub.entity.BackgroundEntity;
import oakclub.global.Container;
import oakclub.global.Context;
import oakclub.model.UserModel;
import oakclub.service.ScribeService;
import org.gearman.GearmanFunction;
import org.gearman.GearmanFunctionCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginBackground extends BaseBackground implements GearmanFunction
{
    private final Logger logger = LoggerFactory.getLogger(LoginBackground.class);

    @Override
    public byte[] work(String s, byte[] bytes, GearmanFunctionCallback gearmanFunctionCallback) throws Exception
    {
        this.logger.info("LoginBackground");
        this.execute(bytes, new IExecute()
        {
            @Override
            public void execute(BackgroundEntity entity) throws Exception
            {
                // params
                long userId = (long) entity.getParam("user_id");
                String deviceName = (String) entity.getParam("device_name");
                String osVersion = (String) entity.getParam("os_version");
                String deviceToken = (String) entity.getParam("device_token");

                // update user info
                UserModel userModel = (UserModel) Container.getComponent(UserModel.class);
                userModel.login(userId, deviceToken);

                // log
                ScribeService scribeService = (ScribeService) Container.getComponent(ScribeService.class);
                scribeService.login(userId, Context.get().getPlatformId(), deviceName, osVersion);
            }
        });
        return null;
    }
}
